package cn.rtsp.controller;

import cn.rtsp.service.RstpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;


/**
 * @author yliu
 */
@Component
@Slf4j
public class RtspWebSocket extends TextWebSocketHandler {

    /**
     * 客户端会话
     */
    private WebSocketSession session;

    /**
     * session
     *
     * @return
     */
    public WebSocketSession getSession() {
        return session;
    }

    private static RstpService rstpService;

    @Autowired
    public void setRstpService(RstpService rstpService) {
        RtspWebSocket.rstpService = rstpService;
    }

    //建立连接
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info("【建立连接】:【{}】", session);
    }

    //发送消息
    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.info("【接收消息】:【{}】【{}】", session, message);
        String payload = message.getPayload();
        if ("go".equals(payload)) {
            log.info("开始传送实时rstp流~");
            rstpService.startRstp(session);
        }

    }

    //连接关闭
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        log.info("【连接关闭】:【{}】【{}】", session, status);
    }


}
